<template>
  <div class="xiecheng">
    <div class="admin_main_block">
      <div class="admin_main_block_top">
        <div class="admin_main_block_left">
          <div>
            <el-button
              type="success"
              icon="Plus"
              @click="router.push({ name: 'module_organization_form' })">
              {{ i18n('common.create') }}
            </el-button>
          </div>
        </div>
        <div class="admin_main_block_right">
          <div>
            <el-button
              type="danger"
              icon="Delete"
              @click="deleteHandle()">
              {{ i18n('common.batch_delete') }}
            </el-button>
          </div>
        </div>
      </div>

      <div class="admin_main_block_select">
        <div class="input">
          <el-input v-model="dataForm.title" :placeholder="i18n('common.please_input') + i18n('organization.title')" clearable>
          </el-input>
        </div>
        <div class="input">
          <el-select v-model="dataForm.certification_status" :placeholder="$t('common.please_select') + $t('common.certification_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in certificationList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-select v-model="dataForm.audit_status" :placeholder="$t('common.please_select') + $t('common.audit_status')" clearable>
            <el-option :label="i18n('common.all')" value=""></el-option>
            <el-option v-for="(v, k) in auditList" :label="v.text" :key="k" :value="v.value"></el-option>
          </el-select>
        </div>
        <div class="input">
          <el-button icon="Search" @click="getDataList(true)">
            {{ i18n('common.search') }}
          </el-button>
        </div>
      </div>

      <div class="admin_table_main">
        <el-table :data="dataList" v-loading="dataListLoading" @selection-change="selectionChangeHandle" stripe>
          <el-table-column type="selection" header-align="center" align="center">
          </el-table-column>

          <el-table-column prop="id" :label="i18n('common.id')" width="70px">
          </el-table-column>

          <el-table-column prop="organization_no" :label="i18n('organization.organization_no')" width="110">
          </el-table-column>

          <el-table-column :label="i18n('organization.info')">
            <template v-slot="scope">
              <dl class="table_dl">
                <dt>
                  <el-image class="cx-list-image" :src="scope.row.logo">
                    <template v-slot:error>
                      <div class="image-slot">
                        <el-icon><Picture /></el-icon>
                      </div>
                    </template>
                  </el-image>
                </dt>
                <dd class="table_dl_dd_all_30">
                  {{ i18n('organization.title') }}： {{ scope.row.title }}
                </dd>
                <dd class="table_dl_dd_all_16_gray">
                  {{ i18n('common.create_time') }}： {{ scope.row.create_time }}
                </dd>
              </dl>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.squad_total')" width="140">
            <template v-slot="scope">
              <b v-if="scope.row.statistical">{{ scope.row.statistical.squad_total }}</b>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.member_total')" width="140">
            <template v-slot="scope">
              <b v-if="scope.row.statistical">{{ scope.row.statistical.member_total }}</b>
            </template>
          </el-table-column>

          <el-table-column :label="$t('organization.course_total')" width="140">
            <template v-slot="scope">
              <b v-if="scope.row.statistical">{{ scope.row.statistical.course_total }}</b>
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.certification_status')" width="80">
            <template v-slot="scope">
              {{ scope.row.certification_status.text }}
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.audit_status')" width="80">
            <template v-slot="scope">
              {{ scope.row.audit_status.text }}
            </template>
          </el-table-column>

          <el-table-column :label="$t('common.status')" width="80">
            <template v-slot="scope">
              <el-switch
                v-model="scope.row.status.value"
                active-value="1"
                inactive-value="2"
                size="large"
                inline-prompt
                :active-text="$t('common.enable')"
                :inactive-text="$t('common.disable')"
                style="--el-switch-on-color: #13ce66; --el-switch-off-color: #ff4949"
                @change="handleStatus($event, scope.row.id, 'status')">
              </el-switch>
            </template>
          </el-table-column>

          <el-table-column :label="i18n('common.handle')" fixed="right" width="570">
            <template v-slot="scope">
              <el-button
                type="info"
                icon="View"
                @click="router.push({ name: 'module_organization_view', query: { id: scope.row.id } })">
                {{ i18n('common.view') }}
              </el-button>

              <el-button
                type="primary"
                icon="Edit"
                @click="router.push({ name: 'module_organization_form', query: { id: scope.row.id } })">
                {{ i18n('common.update') }}
              </el-button>

              <el-button
                class="el-button--yellow"
                icon="Notebook"
                @click="router.push({ name: 'module_organization_course', query: { id: scope.row.id } })">
                {{ i18n('organization.course_title') }}
              </el-button>

              <el-button
                v-if="scope.row.certification_status.value != 1"
                class="el-button--cyan"
                icon="ZoomOut"
                @click="$router.push({name: 'module_organization_certification', query: {id: scope.row.id}})">
                {{ $t('common.certification') }}
              </el-button>

              <el-button
                v-if="scope.row.audit_status.value != 1"
                class="el-button--purple"
                icon="Finished"
                @click="$router.push({name: 'module_organization_audit', query: {id: scope.row.id}})">
                {{ $t('common.audit') }}
              </el-button>

              <el-button
                type="danger"
                icon="Delete"
                @click="deleteHandle(scope.row.id)">
                {{ i18n('common.delete') }}
              </el-button>
            </template>
          </el-table-column>
        </el-table>
        <div class="admin_table_main_pagination">
          <el-pagination
            @size-change="sizeChangeHandle"
            @current-change="currentChangeHandle"
            :page-size="pageSize"
            :total="totalPage"
            :current-page="pageIndex"
            background
            layout="prev, pager, next,jumper,total">
          </el-pagination>
        </div>
      </div>
    </div>
  </div>
</template>

<script setup name="organization-list">
  import { useBase } from "@/hooks/base/useBase"
  import { useTable } from "@/hooks/table/useTable"

  const { router, i18n, isAuth } = useBase()

  const model = ref('organization')

  const certificationList = ref([])
  const auditList = ref([])

  const dataForm = reactive([
    'title',
    'certification_status',
    'audit_status',
  ])

  const {
    dataList,
    pageIndex,
    pageSize,
    totalPage,
    dataListLoading,
    sizeChangeHandle,
    currentChangeHandle,
    selectionChangeHandle,
    getDataList,
    handleStatus,
    deleteHandle,
  } = useTable(model.value, dataForm)


  const loadCertificationList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'certification_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        certificationList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  const loadAuditList = () => {
    $http({
      url: $http.adornUrl('/dictionary/data'),
      method: 'get',
      params: $http.adornParams({
        title: 'audit_status',
      }),
    }).then(({data}) => {
      if (data && data.status === 200) {
        auditList.value = data.data
      } else {
        $message.error(data.message)
      }
    })
  }

  onMounted(() => {
    getDataList(model)

    loadCertificationList()
    loadAuditList()
  })
</script>
